a tool for shared writing and social publishing
1"use client";
2import Link from "next/link";
3import { useEntitySetContext } from "../../../components/EntitySetProvider";
4import { ActionButton } from "components/ActionBar/ActionButton";
5import { useSearchParams } from "next/navigation";
6import { useIdentityData } from "../../../components/IdentityProvider";
7import { useReplicache } from "src/replicache";
8import { addLeafletToHome } from "actions/addLeafletToHome";
9import { useSmoker } from "../../../components/Toast";
10import { AddToHomeSmall } from "../../../components/Icons/AddToHomeSmall";
11import { HomeSmall } from "../../../components/Icons/HomeSmall";
12import { produce } from "immer";
13
14export function HomeButton() {
15 let { permissions } = useEntitySetContext();
16 let searchParams = useSearchParams();
17
18 return (
19 <>
20 <Link
21 href="/home"
22 prefetch
23 className="hover:no-underline"
24 style={{ textDecorationLine: "none !important" }}
25 >
26 <ActionButton icon={<HomeSmall />} label="Go Home" />
27 </Link>
28 {<AddToHomeButton />}
29 </>
30 );
31}
32
33const AddToHomeButton = (props: {}) => {
34 let { permission_token } = useReplicache();
35 let { identity, mutate } = useIdentityData();
36 let smoker = useSmoker();
37 if (
38 identity?.permission_token_on_homepage.find(
39 (pth) => pth.permission_tokens.id === permission_token.id,
40 ) ||
41 !identity
42 )
43 return null;
44 return (
45 <ActionButton
46 onClick={async (e) => {
47 await addLeafletToHome(permission_token.id);
48 mutate((identity) => {
49 if (!identity) return;
50 return produce<typeof identity>((draft) => {
51 draft.permission_token_on_homepage.push({
52 created_at: new Date().toISOString(),
53 archived: null,
54 permission_tokens: {
55 ...permission_token,
56 leaflets_to_documents: [],
57 leaflets_in_publications: [],
58 },
59 });
60 })(identity);
61 });
62 smoker({
63 position: {
64 x: e.clientX + 64,
65 y: e.clientY,
66 },
67 text: "Leaflet added to your home!",
68 });
69 }}
70 icon={<AddToHomeSmall />}
71 label="Add to Home"
72 />
73 );
74};